package cn.mini.mybatis.mybatis.jdbc;

import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@Slf4j
public class MySQLConnectionUtil {
    public static Connection getConnection() {
        // 数据库连接 URL
        String url = "jdbc:mysql://127.0.0.1:3306/test_db?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&tcpKeepAlive=true&autoReconnect=true&allowMultiQueries=true&nullCatalogMeansCurrent=true";
        // 数据库用户名
        String username = "root";
        // 数据库密码
        String password = "root";

        try {
            // 建立数据库连接
            Connection connection = DriverManager.getConnection(url, username, password);
            log.info ("MySQL 连接成功！");
            return connection;
        } catch (SQLException e) {
            System.out.println("连接失败: " + e.getMessage());
        }
        return null;
    }
}
